import { createRouter, createWebHistory } from "vue-router";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style
/**
 * 创建路由对象
 */
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/login",
      component: () => import("@/views/login.vue"),
      meta: {
        title: "登录",
      },
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    //新页面滚动到顶部，后退时滚动到原来的位置
    return { top: savedPosition?.top || 0 };
  },
});

router.beforeEach(async (to, from, next) => {
  console.log(to);
  next();
});

// 全局解析守卫: 同时在所有组件内守卫和异步路由组件被解析之后 和beforeEach区别是在导航被确认之前
router.beforeResolve((to, from) => {});

// 后置守卫：路由跳转之后
router.afterEach((to, from) => {
  // removeKeepAlive(to, from);
  NProgress.done();
});

router.onError((error) => {
  console.error("[router error] => ", error);
});

export default router;
